
package linuxbox.download.servlet;

import java.io.*;
import java.util.*;
import javax.servlet.http.*;
import java.sql.Timestamp;

import linuxbox.BoxStatic;
import linuxbox.util.*;
import linuxbox.common.*;
import linuxbox.download.logic.*;

public class DownloadAddAction extends Action {
	protected static String successJSP = null;
	
    public void doAction(HttpSession aSession, BoxSession aBoxSession, 
    HttpServletRequest req, HttpServletResponse res) throws Exception {
		BoxStatic.logger.debug("In DownloadAddAction ... ");
		
		String FileDir = BoxStatic.HOME_DIR + BoxStatic.DOWNLOAD_FILE_DIR;
		MultipartRequest multi = null;
		multi = new MultipartRequest(req, FileDir);
		String filename = "";
	
		// Get the list of files to be uploaded
		Enumeration files = multi.getFileNames();
	
		// Right now, we only support one file at a time
		if (files.hasMoreElements()) {
			// Get the name of the file
			String name = (String)files.nextElement();
	
			// Get the file system name of the file
			filename = multi.getFilesystemName(name);
			BoxStatic.logger.debug("FILENAME: " + filename);
			File f = multi.getFile(name);
			int FileLength = (int)f.length();		

		    String downloadCatID = multi.getParameter("DL_CAT_ID");
		    String title = multi.getParameter("FILE_TITLE");
		    String desc = multi.getParameter("DESC");
		    
			BoxStatic.logger.debug("downloadCatID is " + downloadCatID);
		    BoxStatic.logger.debug("title is " + title);
		    
			DownLoadObject dlo = new DownLoadObject(title, 
				Integer.parseInt(downloadCatID), filename, 
				FileLength, new Timestamp(System.currentTimeMillis()));

			DownLoadManager dm = DownLoadManager.getInstance();
			dm.addDownLoad(dlo);
		}

		redirectFile(successJSP, req, res);
	}


	public void setSuccessJSP(String str) {
		successJSP = str;
	}
}
